
/**
 * @author xxxx.ss
 * @description 全局的路由控制文件
 */
import Vue from "vue"
import Router from 'vue-router'

// 引入store实例对象 -- 因为不是一个vue组件，所以就没有this(vue实例)，就没有$store，又因为this.$store实际上就是Store实例对象，所以直接引入使用
import store from '../store'

Vue.use(Router)

// 引入页面组件
import Login from '../views/login'
import NotFound from '../views/common/NotFound'
import Home from '../views/home'

import ProjectManagement from '../views/project'
import ProjectEdit from '../views/project/Edit'

import PurchaseManagement from '../views/purchase'
import PurchaseEdit from '../views/purchase/Edit'
import PurchaseDetail from '../views/purchase/Detail'

import Setting from '../views/setting'

const router = new Router({
    // 模式配置
    mode: 'history',
    // 配置路由
    routes: [
        // 系统默认重定向路由
        {path: '/', redirect: '/login'},
        {path: '/login', component: Login, meta: {notLogin: true, layout: 'blank-layout'}},
        // 添加一个别名，来代替path -- name
        {
            path: '/home',
            name: 'myhome',
            component: Home,
            meta: {title: '凌动智能采购系统', home: false}
        },
        {path: '/project/list', component: ProjectManagement},
        {path: '/project/edit', name: 'editProject', component: ProjectEdit},
        {path: '/purchase/list', component: PurchaseManagement},
        {path: '/purchase/edit', component: PurchaseEdit},
        {path: '/purchase/detail', component: PurchaseDetail},
        {path: '/setting', component: Setting},
        {path: '*', component: NotFound}
    ]
})

// RangeError: Maximum call stack size exceeded -- 路由跳转栈溢出
// 路由权限拦截 -- 没有登录的用户不能访问一些内部业务
router.beforeEach(function(to, from, next) {
    // 通过路由的固定参数，判断，如果为真则不需要进行权限检测
    if (to.meta.notLogin) {
        next()
        return
    }

    // 检测登录标识，如果有则继续业务，没有则重定向到登录业务
    // let token = 'token'
    // 调用store实例直接获取vuex中的缓存数据
    let token = store.getters['common/token']
    if (!!token) {
        next()
    } else {
        next('/login')
    }
})

export default router